package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caocaog.market.annotation.IgnoreAuth;
import com.caocaog.market.entity.ShopChannel;
import com.caocaog.market.entity.SiteChannel;
import com.caocaog.market.service.IShopBasicService;
import com.caocaog.market.service.IShopChannelService;
import com.caocaog.market.service.ISiteChannelService;
import com.caocaog.market.vo.ShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "shopApi", tags = "API-sdx店铺列表")
@RequestMapping("/api/shop/sdx")
public class ApiSDXShopController {
    @Autowired
    private IShopChannelService shopChannelService;
    @Autowired
    private IShopBasicService shopBasicService;
    @Autowired
    private ISiteChannelService siteChannelService;

    //首页 频道
    @IgnoreAuth
    @AutoLog(value = "首页一级频道")
    @ApiOperation(value = "首页一级频道", notes = "首页一级频道")
    @GetMapping(value = "/channelList")
    public Result<?> channelList(ShopChannel shopChannel, HttpServletRequest req) {
        QueryWrapper<ShopChannel> queryWrapper = QueryGenerator.initQueryWrapper(shopChannel, req.getParameterMap());
        List<ShopChannel> list = new ArrayList<>();
        if (oConvertUtils.isNotEmpty(shopChannel.getSiteId())) {
            List<SiteChannel> siteChannelList = siteChannelService.list(new QueryWrapper<SiteChannel>()
                    .eq("site_id", shopChannel.getSiteId())
                    .orderByDesc("sequence"));
            List<String> ids = siteChannelList.stream().map(SiteChannel::getChannelId).collect(Collectors.toList());
            List<String> pids = shopChannelService.listByIds(ids).stream().map(ShopChannel::getParentId).collect(Collectors.toList());

            queryWrapper.in("id", pids);
        }
        if (oConvertUtils.isEmpty(shopChannel.getParentId())) {
            queryWrapper.eq("parent_id", "0");
        }
        queryWrapper.eq("del_flag", 0);
        queryWrapper.eq("is_index", 1);
        list = shopChannelService.queryTreeListNoPage(queryWrapper);
        return Result.OK(list);
    }

    // 根据频道筛选 门店的距离显示排序
    @AutoLog(value = "根据频道id筛选")
    @ApiOperation(value = "根据频道id筛选 门店的距离显示排序", notes = "id:频道id必传\n 1.如果是根据一级频道查会返回下面的二级频道集合和(频道name='全部')店铺集合信息\n" +
            " 2.如果是根据二级频道(除'全部')查只会返回店铺集合",response = ShopVo.class)
    @GetMapping(value = "/shopListByChannelId")
    @IgnoreAuth
    public Result<?> shopListByChannelId(@RequestParam("id") String id,
                                         @RequestParam("lat") String lat,
                                         @RequestParam("lng") String lng) {
        return shopBasicService.shopListByChannelIdForSDX(id,lat,lng);
    }

}
